public without sharing class CaseCommentsComponentController {
	public Id caseId {get; set;}
	public Id parentCaseId {get; set;}

	//get the combined comments for all related cases!
	public cComments[] comments {
		get{
			List<cComments> relatedComments = new List<cComments>();

			Id searchCaseId;
			if(parentCaseId != null)
				searchCaseId = parentCaseId;
			else
				searchCaseId = caseId;

			// Grab the current user profile outside of the for loop so we only query it once.
			Profile currentUserProfile = [Select PermissionsModifyAllData, PermissionsEditCaseComments from Profile where Id = :UserInfo.getProfileId()];

			for(CaseComment comment : [Select Parent.Id, Parent.CaseNumber, Parent.RecordType.Name, Parent.Owner.Name, Parent.Status, LastModifiedDate, LastModifiedBy.Id, LastModifiedBy.Name, IsPublished, CreatedDate, CreatedBy.Id, CreatedBy.Name, CommentBody From CaseComment c where parent.ParentId = :searchCaseId or parentid = :searchCaseId order by c.LastModifiedDate desc])
			{
				cComments tempcComment = new cComments();
				tempcComment.cComment = comment;

				// Build String to display.
				tempcComment.commentText = '<b>Created By: <a href=\'/' + comment.CreatedBy.Id + '\'>' + comment.CreatedBy.Name + '</a> (' + comment.CreatedDate.format() + ') ';

				//show modified date only if it was modified..
				if(comment.LastModifiedDate != comment.CreatedDate){
				   tempcComment.commentText += '| Last Modified By: <a href=\'/' + comment.LastModifiedBy.Id + '\'>' +  comment.LastModifiedBy.Name + '</a> (' + comment.LastModifiedDate.format() + ')';
				}
			//	tempcComment.commentText += '</b><br>' + comment.CommentBody;
                tempcComment.commentText += '</b><br>' + comment.CommentBody.replaceAll('\r\n','</br>');
				
				if(comment.IsPublished)
					tempcComment.PublicPrivateAction = 'Make Private';
				else
					tempcComment.PublicPrivateAction = 'Make Public';

				tempcComment.IsEdit = IsEditComment(currentUserProfile, comment.CreatedBy.Id);
				//Add to list
				relatedComments.add(tempcComment);
			}

			if (relatedComments.size() == 0)
				return null;
    	    else
				return relatedComments;
		}

		set;
	}

	private boolean IsEditComment(Profile currentUserProfile, Id commentOwner)
	{
		//if Logged in user has “Modify All” permission, the “Edit/Del” link display always display.
		if (currentUserProfile.PermissionsModifyAllData)
			return true;
		//Else- if Edit Case Comments permission is checked and if the current user is the owner of this comment.
		if (currentUserProfile.PermissionsEditCaseComments && commentOwner == UserInfo.getUserId())
			return true;

		//if not, do not display the edit/del links
		return false;
	}

	public PageReference NewComment()
	{
		//PageReference newPage = new PageReference('/00a/e?parent_id='+ caseId + '&retURL=%2F' + caseId);
		//newPage.setRedirect(true);
		//This has been moved to component code due to iframe issue. we cannot set _target inside this method!
		return null;
	}

	public PageReference deleteComment()
	{
		Id commentId = ApexPages.currentPage().getParameters().get('CommentId_d');

		for(cComments Comment : comments)
		{
			if(Comment.cComment.Id == commentId)
			{
				delete Comment.cComment;
				break;
			}
		}

		PageReference pg = new PageReference('/' + caseId);
		pg.setRedirect(true);
		return pg;
	}

	public PageReference makePublicPrivate()
	{
		Id commentId = ApexPages.currentPage().getParameters().get('CommentId_p');
		for(cComments Comment : comments)
		{
			if(Comment.cComment.Id == commentId)
			{
				Comment.cComment.IsPublished = !Comment.cComment.IsPublished;
				if(Comment.cComment.IsPublished)
					Comment.PublicPrivateAction = 'Make Private';
				else
					Comment.PublicPrivateAction = 'Make Public';

				update Comment.cComment;
				break;
			}
		}
		PageReference pg = new PageReference('/' + caseId);
		pg.setRedirect(true);
		return pg;
	}

	public class cComments {
		public CaseComment cComment {get; set;}
		public boolean IsEdit {get; set;}
		public String CommentText {get; set;}
		public String PublicPrivateAction {get; set;}
	}
}